#include "ebuildsitemmodel.h"
//#include <QDebug>


////////////////////////////////////////////////////////////////////////////////////////////


EbuildsItemModel::EbuildsItemModel ( QObject *parent )
		: QAbstractListModel ( parent ), packets ( new PacketList ( this ) )  {}

EbuildsItemModel::~EbuildsItemModel() {}

void EbuildsItemModel::initPackets ( const QString &category ) {
	packets->initPackets(category);
}

int EbuildsItemModel::columnCount ( const QModelIndex &parent ) const {
	return 5;
}

QVariant EbuildsItemModel::data ( const QModelIndex &index, int role ) const {
	if ( !index.isValid() )
		return QVariant();

	if ( role == Qt::DisplayRole ) {
		PacketItem *item = static_cast<PacketItem*> ( index.internalPointer() );
		switch ( index.column() ) {
			case 1:
				return item->getPacketName();
		}

	}
	return QVariant();
}

Qt::ItemFlags EbuildsItemModel::flags ( const QModelIndex &index ) const {
	if ( !index.isValid() )
		return 0;

	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant EbuildsItemModel::headerData ( int section, Qt::Orientation orientation,
										int role ) const {
	if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
		switch ( section ) {
			case 0:
				return tr ( "Status" );
			case 1:
				return tr ( "Packet" );
			case 2:
				return tr ( "Installed Version" );
			case 3:
				return tr ( "Lastest Version" );
			case 4:
				return tr ( "Description" );
			default:
				return QVariant();
		}
	}
	return QVariant();
}

QModelIndex EbuildsItemModel::index ( int row, int column, const QModelIndex &parent ) const {
	if ( !hasIndex ( row, column, parent ) )
		return QModelIndex();

	PacketItem *item = packets->getPacket ( row );
	if ( item ) {
		return createIndex ( row, column, item );
	}
	return QModelIndex();
}

int EbuildsItemModel::rowCount ( const QModelIndex &parent ) const {
	return packets->count();
}



